﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using NetWork.Layer;

namespace UI
{
    public class UICopyAwardEvent :MonoBehaviour
    {
        #region ui event
        public void OnSubmitClick()
        {
            // todo 
            GetComponent<UICopyAward>().Close();
        }

        public int currIndex = -1;

        public void OnGetBoxClick(int index)//宝箱1
        {
            Debug.Log("Click seq:" + index);
            currIndex = index;

            GetStarAward(index);
            /*

            var chapterId = GetComponent<UICopyAward>().currentChapterId;

            var chapter = UICopyData.GetUIChapterData( chapterId );
            
            int stars = UICopyData.GetChapterStar( chapterId );

            if(chapter.chapter_box_condition[index] <= stars)
            {
                int minStar = chapter.chapter_box_condition[index];
                currIndex = index;
                GetStarAward(minStar);
#if UNITY_EDITOR
                Debug.Log("[DEBUG] send get star:" + minStar);
#endif 
            }
            else
            {
#if UNITY_EDITOR
                Debug.Log( "[DEBUG] star in not enough need star:"+chapter.chapter_box_condition[index]+" current star:"+stars );
#endif
            }*/

        }


        public void OnMaskClick()
        {
            GetComponent<UICopyAward>().Close();
        }

        #endregion

        #region network event
        private void GetStarAward(int star)
        {
            CSCopyGetStarAward copyStarAward = new CSCopyGetStarAward();
            copyStarAward.starIndex = star;
            copyStarAward.chapterId =  GetComponent<UICopyAward>().currentChapterId;

            NetAPI.SendHttp( OpDefine.CSCopyGetStarAward, copyStarAward, ( data, ok ) =>
            {
                if( ok )
                {
                    SCCopyGetStarAward award = (SCCopyGetStarAward)data.kBody;
                    //UIDropSimpleParser parser = new UIDropSimpleParser( award.gain );

                    GetComponent<UICopyAward>().SetAwardGet(currIndex);


                    var chapterId = GetComponent<UICopyAward>().currentChapterId;

                    var chapter = UICopyData.GetUIChapterData(chapterId);

                    if (currIndex == 0)
                    {
                        chapter.chapter_inst_get_box_0 = true;
                    }
                    else if (currIndex == 1)
                    {
                        chapter.chapter_inst_get_box_1 = true;
                    }
                    else if(currIndex == 2)
                    {
                        chapter.chapter_inst_get_box_2 = true;
                    }

                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("receive_reward_success"));

                    //添加物品
                    StaticData.playerData.AddResultObj( award.gain );
                }
            } );
        }
        #endregion
    }
}